#include "cclientbook.h"
#include <QDataStream>
#include <QTcpSocket>
#include "protocol/ClientBook.h"
#include "protocol/srvdriverans.h"
#include <QtEndian>
CClientBook::CClientBook(QObject *parent)
    :INetWorking(parent)
{
}



CClientBook::~CClientBook()
{
}

void CClientBook::prepareWriteData(QByteArray* block, void* param)
{
    // we cannot discrete this definition to the outside of the block,
    // and pass the reference to the funtion, or it will cause some
    // unknown problems, such as wrong streams. Maybe it is a bug of QT.
    QDataStream out(block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_3);


    protocal::ClientBook* pClientBook = (protocal::ClientBook*)param;

    // output the head information
    out << (qint32)(pClientBook->source) \
        << (qint32)(pClientBook->cli_command) \
        << (qint32)(pClientBook->size);

    // output the body information

    char * temp = new char [10];
    // we must do so, because the constructor of QString(char*) will truncate the
    // remaining space after '\0', and we need them to occupy the space.
    memcpy(temp, pClientBook->userID, 10);
    //out << QString(temp);
    out << temp;
    qDebug(temp);
    out << pClientBook->waitingTime; //minutes

    out << pClientBook->startGPS.lat << pClientBook->startGPS.lon << pClientBook->startGPS.time;
    out << pClientBook->endGPS.lat << pClientBook->endGPS.lon << pClientBook->endGPS.time;



    delete [] temp;
}

bool CClientBook::prepareReadData(QTcpSocket *pTcpSocket, protocal::BasePtc *basePtc)
{
    QDataStream in(pTcpSocket);

    if (   basePtc->source != protocal::SERVER\
        || basePtc->srv_command != protocal::SRV_DRIVER_ANS\
        || basePtc->size != sizeof(protocal::SrvDriverAns)+4*4)
    {
        return false;
    }

    // recieve data, from srvdriverans.h
    char * tmp = new char[10];

    //in >> car_id;
    in >> tmp;
    car_id = QLatin1String(tmp);
    //in >> licence_plate;
    in >> tmp;
    licence_plate = QLatin1String(tmp);
    //in >> driver_name;
    in >> tmp;
    driver_name = QLatin1String(tmp);
    //in >> company;
    in >> tmp;
    company = QLatin1String(tmp);
    in >> honest_degree;
    in >> arrive_time; // in minutes

    // GPS information should be accepted here
    in >> taxiPosition.lat;
    in >> taxiPosition.lon;
    in >> taxiPosition.time;

    delete[] tmp;

    formResult();

    return true;
}

void CClientBook::formResult()
{
    resultMap.insert(QString("car_id"), car_id);
    resultMap.insert(QString("licence_plate"), licence_plate);
    resultMap.insert(QString("driver_name"), driver_name);
    resultMap.insert(QString("company"), company);
    resultMap.insert(QString("honest_degree"), QString::number((int)honest_degree) );
    resultMap.insert(QString("arrive_time"), QString::number((int)arrive_time) );
    resultMap.insert(QString("GPS.lat"), QString::number(taxiPosition.lat));
    resultMap.insert(QString("GPS.lon"), QString::number(taxiPosition.lon));
}
